//
//  ViewController.m
//  通讯录 _自定义cell
//
//  Created by 洛洛大人 on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "Contact.h"
#import "AddressBook.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)AddressBook *addressBook;
@end

@implementation ViewController
-(void)dealloc {
    [_tableView release];
    [_addressBook release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self config];
    [self handleData];
    [self creatTableView];
    
    
}

-(void)config {
    
    self.addressBook = [[AddressBook alloc]init];
    self.navigationItem.title = @"通讯录";
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
}


-(void)handleData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@"plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSArray *keyArray = [dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    for (NSString *key in keyArray) {
        NSArray *contactArray = [dictionary objectForKey:key];
        
        NSMutableArray *arrayOfContact = [NSMutableArray arrayWithCapacity:5];
        
        for (NSMutableDictionary *dic in contactArray) {
            Contact *contact = [[Contact alloc]init];
            [contact setValuesForKeysWithDictionary:dic];
            
            [arrayOfContact addObject:contact];
            
       }
        [self.addressBook.dicOfContact setObject:arrayOfContact forKey:key];
        
        
    }
}

-(void)creatTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    [self.view addSubview:self.tableView];
    
 
    self.tableView.backgroundColor = [UIColor cyanColor];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 100;
    
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *array = [self.addressBook getGroupNames];
    
    NSString *key = [array objectAtIndex:section];
    
    NSArray *array1 = [self.addressBook.dicOfContact objectForKey:key];
    
    return  array1.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.addressBook.dicOfContact.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

   
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.addressBook getGroupNames];
    
    NSArray *contactArray = [self.addressBook.dicOfContact objectForKey:[array objectAtIndex:indexPath.section]];
    
    Contact *contact = [contactArray objectAtIndex:indexPath.row];
    
    cell.cellModel = contact;
    
    return cell;
    
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    
    return [self.addressBook getGroupNames];
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return [[self.addressBook getGroupNames]objectAtIndex:section];
}









@end
